package com.zjzywl.smoke.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import com.zjzywl.smoke.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zjzywl.smoke.entity.SysUser;
import com.zjzywl.smoke.util.WebResult;

@Controller
public class UserActionController extends BaseController {
	@Autowired
	private AppConfig appConfig;

	@RequestMapping(value = "/login")
	public String Login(HttpSession session, Model model, @PathParam("username") String username,
			@PathParam("password") String password) {
		session.setMaxInactiveInterval(-1);
		model.addAttribute("username", username);
		model.addAttribute("password", password);
		if ((username != null) && (username.length() > 0)) {
			model.addAttribute("auto", true);
			session.setAttribute("auto", true);
		} else {
			model.addAttribute("auto", false);
			session.setAttribute("auto", false);
		}

		return "login/login";
	}

	// 查看登陆用户
	@RequestMapping(value = "/user/get")
	@ResponseBody
	public WebResult get() {
		try {
			String userId = this.getUserId();
			ResponseEntity<SysUser> orgs = appConfig.restTemplate()
					.getForEntity(appConfig.getGetDataIp() + "/user/get" + "/" + userId, SysUser.class);
			return WebResult.OK(orgs);
		} catch (Exception e) {
			return WebResult.FAIL(e.getMessage());
		}
	}

	/*
	 * 添加,修改用户
	 */
	@PostMapping(value = "/user/save")
	@ResponseBody
	public WebResult add(SysUser sysUser, @PathParam("orgid") Integer orgid, @PathParam("roleId") Integer roleId) {
		ResponseEntity<SysUser> history = appConfig.restTemplate()
				.postForEntity(appConfig.getGetDataIp() + "/user/save/" + orgid + "/" + roleId, sysUser, SysUser.class);
		return WebResult.OK(history);
	}

	// 获取账号列表，根据path
	@RequestMapping(value = "/get/user/list")
	@ResponseBody
	public WebResult getCurrentUser() {
		String userId = this.getUserId();
		ResponseEntity<SysUser[]> users = appConfig.restTemplate()
				.getForEntity(appConfig.getGetDataIp() + "/get/user/list/" + userId, SysUser[].class);
		return WebResult.OK(users);

	}

	// 删除账号
	@PostMapping(value = "/user/del/{id}")
	@ResponseBody
	public WebResult del(@PathVariable("id") Integer id) {
		ResponseEntity<SysUser> users = appConfig.restTemplate()
				.postForEntity(appConfig.getGetDataIp() + "/user/del/" + id, null, SysUser.class);
		return WebResult.OK(users);
	}
	//账号唯一性验证
	@RequestMapping(value="/user/validateName")
	@ResponseBody
	public ResponseEntity<String> checkUserName(@PathParam("loginName") String loginName, @PathParam("originalloginName") String originalloginName) {
		ResponseEntity<String> users = appConfig.restTemplate()
				.postForEntity(appConfig.getGetDataIp() + "/user/validateName?loginName=" + loginName+"&originalloginName="+originalloginName, null, String.class);
		return users;
		

	}
	//账号唯一性验证
    @RequestMapping(value="/user/out")
    @ResponseBody
    public void UserNameOut(HttpServletResponse response) throws IOException {
        String userId = this.getUserId();
        if(userId.equals("")) {
            response.sendError(403, "Invalid landing, please re-login"); 
        }
        
    }
	
	
}
